﻿[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$farm = [microsoft.sharepoint.administration.spfarm]::local

$Computer = gc env:computername

$WebAppBuilder = new-object microsoft.sharepoint.administration.SPWebApplicationBuilder($farm)
Write-Host "--- Setting up SharePoint Contrib on $Computer ---" -foregroundcolor Green

$Domain = Read-Host "Enter domain"

# Setup webapplication for SharePoint Contrib
Write-Host "Setting up webapplication for SharePoint Contrib" -foregroundcolor Green
$WebAppPort = Read-Host "Enter port (ex 80 7777)"
$WebAppUserName = Read-Host "Enter username"
$WebAppPassword = Read-Host "Enter password" -AsSecureString

$WebAppBuilder.ApplicationPoolId = "SharePoint - $WebAppPort"
$WebAppBuilder.ApplicationPoolUsername = "$Domain\WebAppUserName"
$WebAppBuilder.ApplicationPoolPassword = $WebAppPassword
$WebAppBuilder.Port = $WebAppPort
$WebAppBuilder.ServerComment = "SharePoint - $WebAppPort"
$WebAppBuilder.CreateNewDatabase = $true
$WebAppBuilder.DatabaseServer = Read-Host "Database Server"
$WebAppBuilder.DatabaseName = "WSS_Content - $WebAppPort" 
$WebAppBuilder.RootDirectory = "C:\Inetpub\wwwroot\wss\VirtualDirectories\$WebAppPort"
$WebAppBuilder.UseSecureSocketsLayer = $false
$WebAppBuilder.AllowAnonymousAccess = $false

Write-Host "working..." -foregroundcolor Green
$webapp = $WebAppBuilder.Create()
$webapp.Provision()

Write-Host "OK" -foregroundcolor Green

# Setup sitecollection for SharePoint Contrib
Write-Host "Setting up sitecollection for SharePoint Contrib" -foregroundcolor Green
$SiteLoginName = Read-Host "Enter username for sitecollection"
$SiteUserName = Read-Host "Enter display name for sitecollection user (ex Admininstrator)"

Write-Host "working..." -foregroundcolor Green
$SiteCollection = $webapp.Sites.Add("/sites/sharepointcontrib",
  "SharePoint Contrib",
  "SharePoint Contrib",
  1033,
  "STS#0",
  $SiteLoginName,
  $SiteUserName,
  "someone@email.com")

Write-Host "OK" -foregroundcolor Green

$ApplicationUrl = $SiteCollection.MakeFullUrl($SiteCollection.ServerRelativeUrl)
Write-Host "Setting enviroment variable 'SharePointContribUrl' to '$ApplicationUrl'" -foregroundcolor Green
[Environment]::SetEnvironmentVariable("SharePointContribUrl", $ApplicationUrl, "Machine")
Write-Host "OK" -foregroundcolor Green
Write-Host "--- END ---" -foregroundcolor Green

